package hxy.dragon.location_record.tencent;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.ejlchina.okhttps.HttpResult;
import com.ejlchina.okhttps.OkHttps;

import java.io.IOException;

import hxy.dragon.location_record.MainActivity;

public class NetworkService {

    /*
     * 上次上报的时间
     */
    private static Long lastTime = 0L;

    /**
     * 新增推送到服务器
     *
     * @param locationModel
     * @return
     */
    public static HttpResult postToServer(LocationModel locationModel) {
        Context appContext = MainActivity.getAppContext();
        Long userId = locationModel.getUserId();
        if (userId < 10) {
            Toast.makeText(appContext, "手机号码不正确", Toast.LENGTH_SHORT).show();
            return null;
        }
        long time = locationModel.getTime();
        if (time - lastTime <= 590000) {
            // 不用上报
            //lastTime = time;
            return null;
        }
        lastTime = time;


        return OkHttps.async("/location")
                .bind(appContext)             // 绑定（生命周期|Context获取）
//                            .tag(Tags.TOKEN)        // 自动添加 TOEKN
//                .tag(Tags.LOADING)      // 自动显示加载框
                .setBodyPara(locationModel)
                .bodyType(OkHttps.JSON)
                .setOnResponse(res -> {
//                    editText4.setText(res.toString());
                })
                .setOnException((IOException e) -> {
                    // 这里处理请求异常
                    Log.i("kkk", "异步异常捕获：" + e.getMessage());
                    Toast.makeText(appContext, e.getMessage(), Toast.LENGTH_SHORT).show();
//                    editText4.setText(e.getMessage());
                })
                // 请求参数设置...
                .post().getResult();

    }
}
